﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using System.Web.Security;
using Gift.Utile;
using Gift.Model;
using Gift.DAL;
using System.Text.RegularExpressions;

namespace WebSite.cn
{
    public partial class register : BasePage
    {
        // Fields

        DALMember theMember = new DALMember();

        // Methods
        protected void Button1_Click(object sender, EventArgs e)
        {

                //DataTable table = SQLAccess.GreatDs("select * from DB_Member where memberlogin='" + re.replace(this.usernamea.Value) + "'").Tables[0];
                //if (table.Rows.Count > 0)
                //{
                //    base.Response.Write("<script language='javascript'>alert('对不起，该用户名已存在！');window.history.go(-1);</script>");
                //    base.Response.End();
                //}
                //else
                //{
                //    SQLAccess.DoSql(string.Concat(new object[] { "insert into DB_Member (memberlogin,memberpwd,email,membername,sex,tel1,updatetime,state,rankid) values ('", re.replace(base.Request.Form["usernamea"]), "','", FormsAuthentication.HashPasswordForStoringInConfigFile(re.replace(base.Request.Form["pas"]), "MD5"), "','", re.replace(base.Request.Form["email"]), "','", re.replace(base.Request.Form["cname"]), "','", "", "','", re.replace(base.Request.Form["tel"]), "','", DateTime.Now, "',1,1)" }));
                //   WebSite.login.usercenter(re.replace(base.Request.Form["usernamea"]), FormsAuthentication.HashPasswordForStoringInConfigFile(re.replace(base.Request.Form["pas"]), "MD5"), "0");
                //    base.Response.End();
                //}
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }



        protected void butSubmit_Click(object sender, EventArgs e)
        {
            string strMessage = string.Empty;
            //if (txtCode.Text.Length == 0)
            //{
            //    strMessage += " - 验证码不能为空!<br />";
            //}
            //if (!string.Equals(txtCode.Text, Request.Cookies["dealeradmin"].Value.ToLower()))
            //{
            //    strMessage += " - 验证码输入错误!<br />";
            //}
            if (!string.IsNullOrEmpty(strMessage))
            {
                Message.Msg(this.Page, strMessage, MessageType.Failure);
                return;
            }
            CheckSubmit(txtName.Text, txtPass.Text, txtCheckPass.Text, txtEmail.Text);
        }

        protected void CheckSubmit(string username, string password, string checkPassword, string email)
        {
            string strMessage = string.Empty;
            if (username.Length == 0)
            {
                strMessage += " - 用户名不能为空!<br />";
            }
            else
            {
                if (username.Length < 4 || username.Length > 20)
                {
                    strMessage += " - 用户名(4-20)位数字与字符!<br />";
                }
            }
            if (password.Length == 0)
            {
                strMessage += " - 密　码不能为空!<br />";
            }
            else
            {
                if (password.Length < 4 || password.Length > 20)
                {
                    strMessage += " - 密　码(4-20)位数字与字符!<br />";
                }
                else
                {
                    if (!string.Equals(password, checkPassword))
                    {
                        strMessage += " - 两次密码输入要相同!<br />";
                    }
                }
            }
            if (email.Length == 0)
            {
                strMessage += " - Email不能为空!<br />";
            }
            else
            {
                if (!RegexStr.CheckEmail(email))
                {
                    strMessage += " - Email格式不正确!<br />";
                }
            }
            if (!string.IsNullOrEmpty(strMessage))
            {
                Message.Msg(this.Page, strMessage, MessageType.Failure);
                return;
            }

            DB_Member oMember = new DB_Member();
            oMember.LoginName = username;
            oMember.LoginPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(re.replace(password), "MD5"); 
            oMember.Email = email;
            oMember.IsEnable = true;
            
          
            if (theMember.Add(oMember) > 0)
            {
                LoginMember = oMember;
                Message.Msg(this.Page, "注册成功!", MessageType.Success);
                HttpContext.Current.Response.Write(Gift.Utile.Utile.WrapJavaScript(
                           "setTimeout('window.parent.location.reload()',2000)"));
            }
            else
            {
                Message.Msg(this.Page, " - 用户名已存在, 请更换用户名!<br />", MessageType.Failure);
            }
        }

        // Properties


    }
}